package com.app.nba.web;

import com.app.nba.pojo.LoginVO;
import com.app.nba.pojo.User;
import com.app.nba.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class PageController {

    @GetMapping("/")
    public String getLogins() {
        return "login";
    }

    @GetMapping("index")
    public String getIndex(HttpSession session, Model model) {
        LoginVO user = (LoginVO) session.getAttribute("user");
        model.addAttribute("user", user);
        return "index";
    }

    @GetMapping("login")
    public String getLogin() {
        return "login";
    }

    @GetMapping("message")
    public String getMessage() {
        return "message";
    }


    @GetMapping("introduction")
    public String getIntroduction(HttpSession session, Model model) {
        LoginVO user = (LoginVO) session.getAttribute("user");
        model.addAttribute("user", user);
        return "introduction";
    }

    @GetMapping("match")
    public String getMatch(HttpSession session, Model model) {
        LoginVO user = (LoginVO) session.getAttribute("user");
        model.addAttribute("user", user);
        return "match";
    }

    @GetMapping("tips")
    public String getTips(HttpSession session, Model model) {
        LoginVO user = (LoginVO) session.getAttribute("user");
        model.addAttribute("user", user);
        return "tips";
    }

    @GetMapping("star_player")
    public String getStar_player(HttpSession session, Model model) {
        LoginVO user = (LoginVO) session.getAttribute("user");
        model.addAttribute("user", user);
        return "star_player";
    }

    @GetMapping("toy")
    public String getToy(HttpSession session, Model model) {
        LoginVO user = (LoginVO) session.getAttribute("user");
        model.addAttribute("user", user);
        return "toy";
    }

    @GetMapping("survey")
    public String getSurvey(HttpSession session, Model model) {
        LoginVO user = (LoginVO) session.getAttribute("user");
        model.addAttribute("user", user);
        return "survey";
    }

    @GetMapping("join")
    public String getJoin(HttpSession session, Model model) {
        LoginVO user = (LoginVO) session.getAttribute("user");
        model.addAttribute("user", user);
        return "join";
    }

    @Autowired
    private UserService userService;

    @GetMapping("userInfo")
    public String getUserInfo(HttpSession session, Model model) {
        LoginVO user = (LoginVO) session.getAttribute("user");
        User data = new User();
        data.setAccount(user.getAccount());
        data.setPassword(user.getPassword());
        User user1 = userService.selectOne(data);
        model.addAttribute("user", user1);
        return "userInfo";
    }

    @PostMapping("edit")
    public String editUser(HttpSession session, Model model, User user) {
        User data = userService.selectOne(user);
        LoginVO vo = new LoginVO();
        //账号不重复
        if (data == null) {
            userService.edit(user);
            user = userService.findById(user.getUserId());
            vo.setAccount(user.getAccount());
            vo.setPassword(user.getPassword());
            session.setAttribute("user", vo);
            model.addAttribute("msg", "编辑成功！");
            return "message";
            //账号重复
        } else {
            User account = userService.checkAccount(user);
            //是当前账号
            if (account != null) {
                userService.edit(user);
                user = userService.findById(user.getUserId());
                vo.setAccount(user.getAccount());
                vo.setPassword(user.getPassword());
                session.setAttribute("user", vo);
                model.addAttribute("msg", "编辑成功！");
                //不是当前账号
            } else {
                model.addAttribute("msg", "编辑失败，账号重复！");
            }
            return "message";
        }
    }
}
